

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class TicketPan extends JPanel {

	  private Image img;
	  
	  
	  public TicketPan(){
		  
	  }

	  public void setImage(String img) {
		this.removeAll();
		
	    try {
			setImage(ImageIO.read(new File(img)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	  }

	  public void setImage(Image img) {
	    this.img = img;
	    
	  }

	  protected  void paintComponent(Graphics g) {
		super.paintComponent(g);
	    g.drawImage(img, 0, 0, null);
	  }
	  
	  /**
	   * 
	   * @param srcImageFile
	   * @param scale
	   * @param flag true 放大  false 縮小
	   */
	  private Image scale(String srcImageFile, double width, double height)  {  
		  double newwidth=0, newheight=0;
		  double divide;
	        try{  
	            BufferedImage src = ImageIO.read(new File(srcImageFile));
	            if(src.getHeight() > src.getWidth()){
		            newheight = height;
		            divide = height / src.getHeight();
		            newwidth = src.getWidth() * divide;
	            }else{
	            	newwidth = width;
	            	divide = width / src.getWidth();
	            	newheight = src.getHeight() * divide;
	            }
	            Image image = src.getScaledInstance((int)newwidth, (int)newheight, Image.SCALE_DEFAULT); 
	            return image;	       
	        }catch (IOException e){  
	            e.printStackTrace();  
	            return new ImageIcon(srcImageFile).getImage();
	        }  
	  }  
 
	  public void clearImg(){
		  if(img != null)
			  img.flush();
		  img = null;
	  }

}
